#!/bin/bash
#==========================================================================
#==========================================================================
#
#  The Sick LIDAR Matlab/C++ Toolbox
#
#  File: build_mex
#  Auth: Jason Derenick and Christopher Mansley
#  Cont: jasonder(at)seas(dot)upenn(dot)edu
#  Date: 28 September 2009
#
#  In:   BUILD_TYPE: 'ld','lms1xx','lms2xx','all' (default: 'all')
#                     or 'clean'
#
#  Desc:
#        This script builds the mex interface for the Sick Toolbox.
#        If no arguments are supplied then it builds all of Sick
#        interfaces.
#
#        Us: build_mex [BUILD_TYPE=all]
#        Ex: build_mex
#        Ex: build_mex lms2xx
#        Ex: build_mex clean
#
#==========================================================================

# Define the temp directories
BUILD_DIR=build;
CONF_DIR=.sickconf;

# Define LMS and LD src dir names
SICK_LD_SRC_DIR=sickld
SICK_LMS_1XX_SRC_DIR=sicklms1xx
SICK_LMS_2XX_SRC_DIR=sicklms2xx

# Default build type
BUILD_TYPE='all';

# Check what to build
if [ -n "$1" ]; then

  # Do they want help?
  if [ $1 = "--help" ]; then  
    echo "usage: $0 [BUILD_TYPE=all]"
    echo "example: $0 lms2xx"
    exit;
  fi

  # Assign the build type
  BUILD_TYPE=$1;

fi

echo
echo "The Sick LIDAR Matlab/C++ Toolbox"
echo "Build Script (Version 1.1)"
echo

# Make sure its a valid option
if [ $BUILD_TYPE = "--help" -o ! \
     $BUILD_TYPE = "ld" -a ! \
     $BUILD_TYPE = "lms1xx" -a ! \
     $BUILD_TYPE = "lms2xx" -a ! \
     $BUILD_TYPE = "all" -a ! \
     $BUILD_TYPE = "clean" ]; then

  echo "usage: bash $0 [BUILD_TYPE]"
  echo "example: bash $0 lms2xx"

  exit;

fi

# Check if its a clean build
if [ $BUILD_TYPE = "clean" ]; then

  echo "Cleaning up..."
  if [ -e ./$BUILD_DIR ]; then
    rm -r ./$BUILD_DIR;
  fi
  echo "       Done cleaning!"

  echo
  echo "*** To BUILD re-run build_mex :o) ***";
  echo 

  exit;

fi

# Read in Matlab bin directory
BIN_DIR=`cat $CONF_DIR/.matbin 2>/dev/null`;
if [ ! $? -eq 0 ]; then

  echo "$0: Couldn't find config file! Please run config_mex."

  # If it exists then kill it
  if [ -e $CONF_DIR ]; then
    rm -r $CONF_DIR;
  fi

  exit;

fi

if [ -z $BIN_DIR ]; then

  echo "$0: Empty config file! Please (re)run config_mex."

  # If it exists then kill it
  if [ -e $CONF_DIR ]; then
    rm -r $CONF_DIR;
  fi

  exit;

fi

# Check whether to make temp build dir
if [ ! -e ./$BUILD_DIR ]; then

  mkdir ./$BUILD_DIR 2>/dev/null;

  if [ ! $? -eq 0 ]; then
    echo "$0: Couldn't make temp directory! Permissions problem?"
    exit;
  fi

fi

if [ $BUILD_TYPE = "all" -o $BUILD_TYPE = "ld" ]; then

  # Build the mex and place in temp dir
  echo "Building Sick LD mex..."

  $BIN_DIR/mex -g -output ./$BUILD_DIR/sickld \
                          interfaces/ld/ldmex.cc \
                          ../c++/drivers/ld/$SICK_LD_SRC_DIR/SickLDMessage.cc \
                          ../c++/drivers/ld/$SICK_LD_SRC_DIR/SickLDBufferMonitor.cc \
                          ../c++/drivers/ld/$SICK_LD_SRC_DIR/SickLD.cc \
                          -I../c++/drivers/ld/$SICK_LD_SRC_DIR \
                          -I../c++/drivers/base/src

  if [ ! $? -eq 0 ]; then
    echo "$0: Build failed!"
    exit;
  fi

  echo "       Build complete!"

  cp ./interfaces/ld/sickld.m ./$BUILD_DIR;

  echo

fi

if [ $BUILD_TYPE = "all" -o $BUILD_TYPE = "lms1xx" ]; then

  # Build the mex and place in temp dir
  echo "Building Sick LMS 1xx mex..."

  $BIN_DIR/mex -g -output ./$BUILD_DIR/sicklms1xx \
                          interfaces/lms1xx/lms1xxmex.cc \
                          ../c++/drivers/lms1xx/$SICK_LMS_1XX_SRC_DIR/SickLMS1xxMessage.cc \
                          ../c++/drivers/lms1xx/$SICK_LMS_1XX_SRC_DIR/SickLMS1xxBufferMonitor.cc \
                          ../c++/drivers/lms1xx/$SICK_LMS_1XX_SRC_DIR/SickLMS1xx.cc \
                          -I../c++/drivers/lms1xx/$SICK_LMS_1XX_SRC_DIR \
                          -I../c++/drivers/base/src

  if [ ! $? -eq 0 ]; then
    echo "$0: Build failed!"
    exit;
  fi

  echo "       Build complete!"

  cp ./interfaces/lms1xx/sicklms1xx.m ./$BUILD_DIR;

  echo

fi

if [ $BUILD_TYPE = "all" -o $BUILD_TYPE = "lms2xx" ]; then

  echo "Building Sick LMS 2xx mex..."

  $BIN_DIR/mex -g -output ./$BUILD_DIR/sicklms2xx \
                          interfaces/lms2xx/lms2xxmex.cc \
                          ../c++/drivers/lms2xx/$SICK_LMS_2XX_SRC_DIR/SickLMS2xxMessage.cc \
                          ../c++/drivers/lms2xx/$SICK_LMS_2XX_SRC_DIR/SickLMS2xxBufferMonitor.cc \
                          ../c++/drivers/lms2xx/$SICK_LMS_2XX_SRC_DIR/SickLMS2xx.cc \
                          -I../c++/drivers/lms2xx/$SICK_LMS_2XX_SRC_DIR/ \
                          -I../c++/drivers/base/src

  if [ ! $? -eq 0 ]; then
    echo "$0: Build failed!"
    exit;
  fi

  echo "       Build complete!"

  cp ./interfaces/lms2xx/sicklms2xx.m ./$BUILD_DIR;

  echo

fi

# Copy supporting file to temp dir
echo "Copying toolbox files..."
cp ./misc/*.xml ./$BUILD_DIR;
cp ./misc/*.gif ./$BUILD_DIR;
echo "       Files copied!"

echo

# Adjust permissions
echo "Adjusting permissions..."
chmod -R 755 ./$BUILD_DIR;
echo "       Permissions set!"

echo
echo "*** To INSTALL run install_mex :o) ***";
echo
